<?php
namespace App\Common\Services;

use App\BaseService;
use App\Common\Models\Area;
use Illuminate\Support\Facades\DB;
use fendui\Feieyun;
class PrinterService extends BaseService
{

    const TYPE_REGISTER = 1; //类型 预约挂号
    const TYPE_DRUGS = 2; //类型 处方单prescription
    const TYPE_PRESCRIPTION = 3; //类型 处方单prescription

    public function __construct(){
        $this->username = 'minhaizhongyi@163.com';
        $this->u_key = 'eNXEvEUc8xng2Zu6';
        $this->feieyun = new Feieyun($this->username, $this->u_key);
    }


    public function addList($params){
        $private_params = [
            'printerContent' => $params['printerContent'],
        ];
        $response_data = $this->feieyun->printerAddlist($private_params);
        //dd($response_data);
        return $response_data;
    }


   public function msgSend($params){


/*       if ($params['type']=1){
           $orderInfo= $this->register($params['data']);
           //dd($private_params);
       }*/


       switch ($params['type']) {
           case PrinterService::TYPE_REGISTER:
               $orderInfo= $this->register($params['data']);
               break;
           case PrinterService::TYPE_DRUGS:
               $orderInfo= $this->drugs($params['data'],14,6,3,6);
               break;
           case PrinterService::TYPE_PRESCRIPTION:
               $orderInfo= $this->prescription($params['data']);
               break;
           default:
               throw new \Exception('打印异常，请联系客服');
               break;
       }

       $private_params = [
               'sn' => $params['sn'],
               'content'=>$orderInfo,
               'times'=>$params['times'],
       ];
       //dd($private_params);
      // die;
       $response_data =  $this->feieyun->printMsg($private_params);
       //dd($response_data);
       return $response_data;
   }



    /*预约挂号单 模版*/
    function register($arr){

        $orderInfo = '<CB>掌无疾北京店预约挂号单</CB><BR>';
        //$orderInfo .= '名称           单价  数量 金额<BR>';
        $orderInfo .= '--------------------------------<BR>';
        $name = $arr['name'];
        $unmber = $arr['unmber'];
        $doctor = $arr['doctor'];
        $time = $arr['time'];
        $pay = $arr['pay'];
        $money = $arr['money'];

        $orderInfo .= '姓  名：'.$name.'<BR>';
        $orderInfo .= '身份证号：'.$unmber.'<BR>';
        $orderInfo .= '预约专家：'.$doctor.'<BR>';
        $orderInfo .= '预约时间：'.$time.'<BR>';
        $orderInfo .= '预约时间：'.$pay.'<BR>';
        $orderInfo .= '挂号费状态：'.number_format($money, 1).'元<BR>';
        $orderInfo .= '--------------------------------<BR>';
        $orderInfo .= '联系电话：020-39004606<BR>';
        $orderInfo .= '医药地址：北京市xx路xx号<BR>';
        $orderInfo .= '备注：当天就诊<BR><BR>';
        $orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
        return $orderInfo;
    }


    /*药品单打印*/
    function drugs($arr,$A,$B,$C,$D){

        /**
         *    飞鹅技术支持
         *    #########################################################################################################
         *
         *    进行订单的多列排版demo，实现商品超出字数的自动换下一行对齐处理，同时保持各列进行对齐
         *
         *    排版原理是统计字符串字节数，补空格换行处理
         *
         *    58mm的机器,一行打印16个汉字,32个字母;80mm的机器,一行打印24个汉字,48个字母
         *
         *    #########################################################################################################
         */

        $orderInfo = '<CB>掌无疾北京店药品支付单</CB><BR>';
        $orderInfo .= '名称           单价  数量 金额<BR>';
        $orderInfo .= '--------------------------------<BR>';
        foreach ($arr as $k5 => $v5) {
            $name = $v5['title'];
            $price = $v5['price'];
            $num = $v5['num'];
            $prices = $v5['price']*$v5['num'];
            $kw3 = '';
            $kw1 = '';
            $kw2 = '';
            $kw4 = '';
            $str = $name;
            $blankNum = $A;//名称控制为14个字节
            $lan = mb_strlen($str,'utf-8');
            $m = 0;
            $j=1;
            $blankNum++;
            $result = array();
            if(strlen($price) < $B){
                $k1 = $B - strlen($price);
                for($q=0;$q<$k1;$q++){
                    $kw1 .= ' ';
                }
                $price = $price.$kw1;
            }
            if(strlen($num) < $C){
                $k2 = $C - strlen($num);
                for($q=0;$q<$k2;$q++){
                    $kw2 .= ' ';
                }
                $num = $num.$kw2;
            }
            if(strlen($prices) < $D){
                $k3 = $D - strlen($prices);
                for($q=0;$q<$k3;$q++){
                    $kw4 .= ' ';
                }
                $prices = $prices.$kw4;
            }
            for ($i=0;$i<$lan;$i++){
                $new = mb_substr($str,$m,$j,'utf-8');
                $j++;
                if(mb_strwidth($new,'utf-8')<$blankNum) {
                    if($m+$j>$lan) {
                        $m = $m+$j;
                        $tail = $new;
                        $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
                        $k = $A - strlen($lenght);
                        for($q=0;$q<$k;$q++){
                            $kw3 .= ' ';
                        }
                        if($m==$j){
                            $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
                        }else{
                            $tail .= $kw3.'<BR>';
                        }
                        break;
                    }else{
                        $next_new = mb_substr($str,$m,$j,'utf-8');
                        if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
                        else{
                            $m = $i+1;
                            $result[] = $new;
                            $j=1;
                        }
                    }
                }
            }
            $head = '';
            foreach ($result as $key=>$value) {
                if($key < 1){
                    $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
                    $v_lenght = strlen($v_lenght);
                    if($v_lenght == 13) $value = $value." ";
                    $head .= $value.' '.$price.' '.$num.' '.$prices;
                }else{
                    $head .= $value.'<BR>';
                }
            }
            $orderInfo .= $head.$tail;
            @$nums += $prices;
        }
        $time = date('Y-m-d H:i:s',time());
        $orderInfo .= '--------------------------------<BR>';
        $orderInfo .= '合计：'.number_format($nums, 1).'元<BR>';
        $orderInfo .= '--------------------------------<BR>';
        $orderInfo .= '联系电话：020-39004606<BR>';
        $orderInfo .= '医药地址：北京市xx路xx号<BR>';
        $orderInfo .= '备注：多休息<BR><BR>';
        $orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
        return $orderInfo;
    }


    function prescription($arr,$A,$B,$C,$D){

        /**
         *    飞鹅技术支持
         *    #########################################################################################################
         *
         *    进行订单的多列排版demo，实现商品超出字数的自动换下一行对齐处理，同时保持各列进行对齐
         *
         *    排版原理是统计字符串字节数，补空格换行处理
         *
         *    58mm的机器,一行打印16个汉字,32个字母;80mm的机器,一行打印24个汉字,48个字母
         *
         *    #########################################################################################################
         */

        $orderInfo = '<CB>掌无疾北京店药品支付单</CB><BR>';
        $orderInfo .= '名称           单价  数量 金额<BR>';
        $orderInfo .= '--------------------------------<BR>';
        foreach ($arr as $k5 => $v5) {
            $name = $v5['title'];
            $price = $v5['price'];
            $num = $v5['num'];
            $prices = $v5['price']*$v5['num'];
            $kw3 = '';
            $kw1 = '';
            $kw2 = '';
            $kw4 = '';
            $str = $name;
            $blankNum = $A;//名称控制为14个字节
            $lan = mb_strlen($str,'utf-8');
            $m = 0;
            $j=1;
            $blankNum++;
            $result = array();
            if(strlen($price) < $B){
                $k1 = $B - strlen($price);
                for($q=0;$q<$k1;$q++){
                    $kw1 .= ' ';
                }
                $price = $price.$kw1;
            }
            if(strlen($num) < $C){
                $k2 = $C - strlen($num);
                for($q=0;$q<$k2;$q++){
                    $kw2 .= ' ';
                }
                $num = $num.$kw2;
            }
            if(strlen($prices) < $D){
                $k3 = $D - strlen($prices);
                for($q=0;$q<$k3;$q++){
                    $kw4 .= ' ';
                }
                $prices = $prices.$kw4;
            }
            for ($i=0;$i<$lan;$i++){
                $new = mb_substr($str,$m,$j,'utf-8');
                $j++;
                if(mb_strwidth($new,'utf-8')<$blankNum) {
                    if($m+$j>$lan) {
                        $m = $m+$j;
                        $tail = $new;
                        $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
                        $k = $A - strlen($lenght);
                        for($q=0;$q<$k;$q++){
                            $kw3 .= ' ';
                        }
                        if($m==$j){
                            $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
                        }else{
                            $tail .= $kw3.'<BR>';
                        }
                        break;
                    }else{
                        $next_new = mb_substr($str,$m,$j,'utf-8');
                        if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
                        else{
                            $m = $i+1;
                            $result[] = $new;
                            $j=1;
                        }
                    }
                }
            }
            $head = '';
            foreach ($result as $key=>$value) {
                if($key < 1){
                    $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
                    $v_lenght = strlen($v_lenght);
                    if($v_lenght == 13) $value = $value." ";
                    $head .= $value.' '.$price.' '.$num.' '.$prices;
                }else{
                    $head .= $value.'<BR>';
                }
            }
            $orderInfo .= $head.$tail;
            @$nums += $prices;
        }
        $time = date('Y-m-d H:i:s',time());
        $orderInfo .= '--------------------------------<BR>';
        $orderInfo .= '合计：'.number_format($nums, 1).'元<BR>';
        $orderInfo .= '--------------------------------<BR>';
        $orderInfo .= '联系电话：020-39004606<BR>';
        $orderInfo .= '医药地址：北京市xx路xx号<BR>';
        $orderInfo .= '备注：多休息<BR><BR>';
        $orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
        return $orderInfo;
    }
}
